<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Requests\ArtlUpdateRequest;
use App\Http\Requests\ArticleRequest;

class ArticleController extends Controller
{
    // 文章展示
    public function getIndex(Request $request)
    {
    	//每页显示
        $num = $request->input('num',8);

        if($request->input('keywords')){

            $artls = DB::table('article')->where('title','like','%'.$request->input('keywords').'%')->orderBy('time','desc')->paginate($num);
            // echo 33;

        }else{

        	 //查询数据
    	if(!empty($request->id)){

    		$artls = DB::table('article')->where('uid',$request->id)->orderBy('time','desc')->paginate($num);
    		// echo 22;
    	}else{

    		$artls = DB::table('article')->orderBy('time','desc')->paginate($num);
    		// echo 11;
        }

        }

		 $data = $request->all();
            // dd($data);
    		// dd($artls);

       // 查询数据
    	$res = DB::table('type')->get();

    	$nm = DB::table('article')->where('id',$request->id)->first();

    	//文章查询

    	return view('Admin.article.index',['res'=>$res,'artls'=>$artls,'data'=>$data,'nm'=>$nm]);
    }

   
  



    //添加文章
   public function getAdd()
    {
        //查询数据库的分类
        $cates = DB::table('type')->get();
        //返回添加post添加表单
        return view('Admin.article.add',['cates'=>$cates]);
    }

    //添加文章操作

    public function postInsert(Request $request)

    public function postInsert(ArticleRequest $request)

    {
        // 接受数据
        $data = $request->except('_token');

        //获取当前时间戳
        $time = time();
       
       $data['time']=$time;


       $data['pic']=$this->upload($request);

       // dd($data);
       //入库操作
       $res = DB::table('article')->insert($data);
       session(['id'=>$data['uid']]);

       if($res){

            return redirect('/admin/artl/index')->with('success','添加成功');
       }else{
            return back()->with('error','添加失败');
       }
    }


    //文件上传
    public function upload($request)
    {
            // $type = $request ->file('pic')->mimeType;
            // dd($request);
        // var_dump($request);
        $arr = ['jpg','png','jpeg','gif'];

        if($request->file('pic'))
        {

            // var_dump($this->$mimeType);
            $t = $request->file('pic')->guessExtension();

            if(!in_array($t,$arr)){
                echo '文件类型不符合';
            }

        //随机文件名
        $name = md5(time()+rand(1,99999));
        //后缀名
        $su = $request->file('pic')->getClientOriginalExtension();
        $request->file('pic')->move('./public/articleuploads', $name.'.'.$su);

        return '/public/articleuploads/'.$name.'.'.$su;


        }
    }



    //删除操作
    public function postDel(Request $request)
    {
    	// dd($request->all());
    	$res = DB::table('article')->where('id','=',$request->id)->delete();
             echo $res;
    }

    //修改
    public function getEdit($id)
    {
    	// echo 22;

    	$data = DB::table('article')->where('id',$id)->first();

    	return view('Admin.article.edit',['data'=>$data]);
    	//
    }

    //修改信息数据库操作

    public  function postUpdate(Request $request)

    public  function postUpdate(ArtlUpdateRequest $request)

    {
    	// dd($request->all());

    	$data = $request->except('_token');


    	$res = DB::table('article')->where('id',$request->id)->update($data);


        
        //文件上传处理
        $data['pic'] = $this->upload($request);
           
    
    	$res = DB::table('article')->where('id',$request->id)->update($data);

    	if($res){
    		return redirect('/admin/artl/index')->with('success','修改成功');
    	}else{
    		return back()->with('error','添加失败');
    	}
    }


    //查看文章内容
    public function getCont($id)
    {
        //查询数据
        $data = DB::table('article')->where('id',$id)->get();

        return view('Admin.article.show',['data'=>$data]);
    }

    //文章状态修改
    public function getChange($id)
    {
        //查询当前文章状态
        $res = DB::table('article')->where('id',$id)->first();

        if($res->status == 1){

            $data['id']=$id;
            $data['status']=0;
            $nums = DB::table('article')->where('id',$id)->update($data);
        }else{
            $data['id']= $id;
            $data['status']=1;
            $nums = DB::table('article')->where('id',$id)->update($data);
        }

        //判断是否成功
        if($nums){
            return redirect('/admin/artl/index')->with('success','文章状态修改成功');
        }else{
            return back()->with('error','文章状态修改失败');
        }
    }

   
}
